//
//  LWAccount.m
//  1.28  weibo
//
//  Created by mac on 15/2/3.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWAccount.h"
#import "NSObject+MJCoding.h"
#define LWAccountFlieName @"account.data"
@implementation LWAccount

//解档和归档
MJCodingImplementation
- (BOOL)save{
   //1.获取沙盒路径
    NSString *accountPath = [LWAccountFlieName appendCacheDir];
    //2.生成过期时间
    NSDate *now = [NSDate date];
    self.expires_time = [now dateByAddingTimeInterval:[self.expires_in doubleValue]];
    //3.保存模型到沙盒
    return [NSKeyedArchiver archiveRootObject:self toFile:accountPath];
}

+ (instancetype)accountFromSandBox{

    //获取沙盒路径
    NSString *accountPath = [LWAccountFlieName appendCacheDir];
    LWAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath];
    NSDate *new = [NSDate date];
    if ([new compare:account.expires_time] != NSOrderedAscending) {
        return nil;
    }

    return account;
}
@end
